<template>
    <!-- 修改资料 -->

    <div>
        <el-form label-width="85px" :model="form" label-position="right" ref="formRef" :rules="formRules">
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="编号" prop="number">
                        <el-input v-model="form.number"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="供应商">
                        <ts-select v-model="form.custId" fileName="ChooseCustReport" :input-width="295"
                            :template="tableProps" ></ts-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="顺序" prop="itemIndex">
                        <el-input-number v-model="form.itemIndex" :min="0" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="附件">
                    <tsUpload @updateFile="updateFile" @removeFile="removeFile"></tsUpload>
                </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="状态" prop="status">
                        <el-switch v-model="form.status" size="large" inline-prompt :active-icon="Check"
                            :inactive-icon="Close" active-value="normal" inactive-value="disable" />
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea"></el-input>
            </el-form-item>
        </el-form>
        <div class="button-position">
            <el-button @click="updateVisible">取 消</el-button>
            <el-button type="primary" @click="addBrand(formRef)">确 定</el-button>
        </div>
    </div>
</template>
  
<script setup>
import { reactive, ref } from "vue";
import { ElNotification } from "element-plus";
import { Check, Close } from '@element-plus/icons-vue';
import tsSelect from '@/components/scSelect/index.vue'
import tsUpload from '@/components/tsUpload/file.vue'
import Api from '@/api/model/pm'
const emit = defineEmits(['updatetable'])
const tableProps = {
    label: 'name',
    value: 'id',
}
// 添加用户数据的对象
const form = ref({
    number: "",
    name: "",
    custId: "",
    status: "normal",
    remark: "",
    itemIndex: 1,
    file:undefined
});
const formRef = ref();
const formRules = reactive({
    number: [{ required: true, message: "请输入编号", trigger: "blur" }],
    name: [{ required: true, message: "请输入名称", trigger: "blur" }],
});
const updateFile = (val) => {
    form.value.file = val
}
const removeFile = () => {
    form.value.file = undefined
}

const updateVisible = (val) => {
    emit('updatetable')
};


const restAddFormData = () => {
    formRef.value.resetFields();
};

// 上传
const addBrand = async (formRef) => {
    await formRef.validate((valid, fields) => {
        if (valid) {
            addBrandData();
        }
    });
};

const addBrandData = async () => {
    // console.log(form.name);
    var params = {
        bean: form.value,
    };
    console.log(form.value)
    var res = await Api.brand.save.post(params);
    console.log(res)
    if (res.code == 200) {
        restAddFormData();
        ElNotification.success({
            title: "",
            message: "添加成功！",
            duration: 2000,
        });
        emit("updatetable", true);
    }
};
</script>
  
<style scoped></style>
  
  